package com.jingxing.controller;

import com.jingxing.config.ApiResult;
import com.jingxing.config.UserLoginToken;
import com.jingxing.pojo.UserInfo;
import com.jingxing.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/userInfo")
@Api(value = "API", tags = {"用户相关"})
@UserLoginToken
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 添加用户信息
     * @param userInfo
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加用户信息")
    public ApiResult add(UserInfo userInfo){
        if(StringUtils.isBlank(userInfo.getName())){
            return ApiResult.parameterError("姓名不能为空");
        }
        if(StringUtils.isBlank(userInfo.getPhone())){
            return ApiResult.parameterError("手机号不能为空");
        }
        boolean save = userInfoService.save(userInfo);
        return ApiResult.ok(save);
    }

    /**
     * 修改用户信息
     * @param userInfo
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改用户")
    public ApiResult update(UserInfo userInfo){
        if(userInfo.getId()==null){
            return ApiResult.parameterError("ID不能为空");
        }
        userInfo.setUpdateTime(new Date());
        boolean update = userInfoService.updateById(userInfo);
        return ApiResult.ok(update);
    }

    /**
     * 删除用户信息
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除用户信息")
    public ApiResult delete(Long id){
        boolean remove = userInfoService.removeById(id);
        return ApiResult.ok(remove);
    }


    /**
     * 查询用户信息
     */
    @PostMapping("/getList")
    @ApiOperation(value = "查询用户信息")
    public ApiResult getList(){
        List<UserInfo> list = userInfoService.list();
        return ApiResult.ok(list);
    }
}
